package cn.xuewenbao.ucenter.service.impl;

import cn.xuewenbao.ucenter.entity.BlackList;
import cn.xuewenbao.ucenter.entity.Member;
import cn.xuewenbao.ucenter.entity.MemberData;
import cn.xuewenbao.ucenter.entity.vo.BlackUserVo;
import cn.xuewenbao.ucenter.entity.vo.BlackVo;
import cn.xuewenbao.ucenter.mapper.BlackListMapper;
import cn.xuewenbao.ucenter.service.BlackListService;
import cn.xuewenbao.ucenter.service.MemberDataService;
import cn.xuewenbao.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.val;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 黑名单 服务实现类
 * </p>
 *
 * @author mafayu
 * @since 2021-06-08
 */
@Service
public class BlackListServiceImpl extends ServiceImpl<BlackListMapper, BlackList> implements BlackListService {

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberDataService dataService;

    //拉黑某用户
    @Override
    public Result<Boolean> addBlack(BlackVo blackVo) {
        Result<Boolean> result = new Result<>();
        String userId = blackVo.getUserId();
        final val blackId = blackVo.getBlackId();
        BlackList blackList = new BlackList();
        blackList.setUserId(userId);
        blackList.setBlackId(blackId);
        int insert = baseMapper.insert(blackList);
        if (insert == 0) {
            result.setSuccess(false);
            result.setResult(false);
            result.setMessage("添加黑名单失败");
            return result;
        }
        result.setSuccess(true);
        result.setResult(true);
        result.setMessage("添加黑名单成功");
        return result;
    }

    //获取黑名单列表
    @Override
    public Result<List<BlackUserVo>> getBlackList(String userId) {
        Result<List<BlackUserVo>> result = new Result<>();
        QueryWrapper<BlackList> blackListQueryWrapper = new QueryWrapper<>();
        blackListQueryWrapper.eq("user_id", userId);
        List<BlackList> blackLists = baseMapper.selectList(blackListQueryWrapper);
        if (blackLists == null) {
            result.setSuccess(false);
            result.setMessage("黑名单为空");
            return result;
        }
        List<BlackUserVo> blackUserVoList=new ArrayList<>();
        for (BlackList blackList : blackLists) {
            BlackUserVo blackUserVo=new BlackUserVo();
            String blackId=blackList.getBlackId();
            Member member=memberService.getById(blackId);
            final val username = member.getUsername();
            final val avatar = member.getAvatar();
            QueryWrapper<MemberData> dataQueryWrapper=new QueryWrapper<>();
            dataQueryWrapper.eq("user_id",blackId);
            MemberData memberData= dataService.getOne(dataQueryWrapper);
            final val fanCount = memberData.getFanCount();
            blackUserVo.setUsername(username);
            blackUserVo.setAvatar(avatar);
            blackUserVo.setFanCount(fanCount);
            blackUserVo.setUserId(blackId);
            blackUserVoList.add(blackUserVo);
        }
        result.setSuccess(true);
        result.setMessage("查找黑名单成功");
        result.setResult(blackUserVoList);
        return result;
    }

    @Override
    public Result<Boolean> removeBlack(BlackVo blackVo) {
        Result<Boolean> result = new Result<>();
        String userId = blackVo.getUserId();
        String blackId = blackVo.getBlackId();
        QueryWrapper<BlackList> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("black_id", blackId);
        int delete = baseMapper.delete(wrapper);
        if (delete == 0) {
            result.setSuccess(false);
            result.setResult(false);
            result.setMessage("移出黑名单失败");
            return result;
        }
        result.setSuccess(true);
        result.setResult(true);
        result.setMessage("移出黑名单成功");
        return result;
    }

}
